package com.sign2024.sign2024project.goodsmaterials.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.goodsmaterials.WzHistoricalRelational;
import com.sign2024.sign2024core.entry.goodsmaterials.WzSpecialtySignRelation;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzHistoricalRelationalMapper;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzSpecialtySignRelationMapper;
import com.sign2024.sign2024project.goodsmaterials.param.WzRelationParam;
import com.sign2024.sign2024project.goodsmaterials.service.WzSpecialtySignRelationService;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物资专家专业与标段对应关系
 *
 * @author yujie
 * @data 2024/8/26 9:33
 */
@Service
public class WzSpecialtySignRelationServiceImpl implements WzSpecialtySignRelationService {

    @Resource
    private WzSpecialtySignRelationMapper wzSpecialtySignRelationMapper;

    @Resource
    private WzHistoricalRelationalMapper wzHistoricalRelationalMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;

    @Override
    public void insertBatchWzSpecialty(List<WzSpecialtySignRelation> list, Integer taskId) {
        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "任务id为空");
        } else {
            // 批量新增
            wzSpecialtySignRelationMapper.insertBatchWzSpecialty(list, taskId);
        }
    }

    @Override
    public void insertBatch(List<WzSpecialtySignRelation> list) {
        // 批量新增
        wzSpecialtySignRelationMapper.insertBatch(list);
    }

    @Override
    public void addWzSpecialtyList(List<WzSpecialtySignRelation> list, String fileName) {

        if (StringUtils.isEmpty(list)) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }

        // 获取历史版本号
        int code = 0;
        Integer versionCode = wzHistoricalRelationalMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }

        // 获取 表示中 所有数据
        List<WzSpecialtySignRelation> relationList = wzSpecialtySignRelationMapper.selectDataAll();
        // 如果 获取的数据 是 空的 说明 当前数据时 第一版本
        if (!relationList.isEmpty()) {
            for (WzSpecialtySignRelation ss : relationList) {
                WzHistoricalRelational h = new WzHistoricalRelational();
                h.setVersionCode(code);
                h.setSectionName(ss.getSectionName());
                h.setMajorOne(ss.getMajorOne());
                h.setMajorTwo(ss.getMajorTwo());
                h.setMajorThree(ss.getMajorThree());
                h.setMajorFour(ss.getMajorFour());
                h.setMajorFive(ss.getMajorFive());
                wzHistoricalRelationalMapper.insert(h);
            }
        } else {
            // 将 第一版数据 添加到数据库中
            for (WzSpecialtySignRelation ss : list) {
                WzHistoricalRelational h = new WzHistoricalRelational();
                h.setVersionCode(code);
                h.setSectionName(ss.getSectionName());
                h.setMajorOne(ss.getMajorOne());
                h.setMajorTwo(ss.getMajorTwo());
                h.setMajorThree(ss.getMajorThree());
                h.setMajorFour(ss.getMajorFour());
                h.setMajorFive(ss.getMajorFive());
                wzHistoricalRelationalMapper.insert(h);
            }
        }

        // 数据清空
        wzSpecialtySignRelationMapper.cleanWzSpecialtySignRelation();


        for (WzSpecialtySignRelation r : list) {
            wzSpecialtySignRelationMapper.insert(r);
        }

        // 添加 回退记录  taskState 任务标识
        RollbackRecord rollback = new RollbackRecord();

        rollback.setFileName(fileName);
        // 任务标识
        rollback.setTaskState(2);
        // 数据 版本号
        rollback.setVersionCode(code);
        rollbackRecordMapper.insert(rollback);
    }

    @Override
    public void deleteByTaskId(Integer taskId) {
        wzSpecialtySignRelationMapper.deleteByTaskId(taskId);
    }

    @Override
    public void cleanWzSpecialtySignRelation() {
        wzSpecialtySignRelationMapper.cleanWzSpecialtySignRelation();
    }

    @Override
    public void rollbackHistorical() {
        // 目前历史回退需要做数据判断 这是的历史回退是 物资 直接 数据写死 为 2
        RollbackRecord vo = rollbackRecordMapper.selectRollbackOne(2);
        // 判断 是否 已回退
        if (vo.getRollbackFlag() == 1) {
            throw new CommonException(CommonConstants.ERROR, "当前数据已回退上一版本，无法回退");
        }

        // 获取 历史版本中 版本号最大的数据
        List<WzHistoricalRelational> historical = wzHistoricalRelationalMapper.selectWzHistoricalByMaxCode();
        if (!historical.isEmpty()) {
            // 数据 不为空
            // 将 这一版半历史数据 赋值到 关系表中
            // 清楚掉当前关系表中的数据
            wzSpecialtySignRelationMapper.cleanWzSpecialtySignRelation();

            for (WzHistoricalRelational ss : historical) {
                WzSpecialtySignRelation h = new WzSpecialtySignRelation();
                h.setSectionName(ss.getSectionName());
                h.setMajorOne(ss.getMajorOne());
                h.setMajorTwo(ss.getMajorTwo());
                h.setMajorThree(ss.getMajorThree());
                h.setMajorFour(ss.getMajorFour());
                h.setMajorFive(ss.getMajorFive());

                wzSpecialtySignRelationMapper.insert(h);
            }
        }

        // 回退成功 将其 修改为已回退
        rollbackRecordMapper.updateRollbackFlag(vo.getId());
    }

    @Override
    public Page<WzSpecialtySignRelation> pageWzSpecialtySignRelation(WzRelationParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<WzSpecialtySignRelation> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<WzSpecialtySignRelation> list = wzSpecialtySignRelationMapper.selectWzRelationByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = wzSpecialtySignRelationMapper.pageWzRelationCount(param);
        page.setTotal(aLong);
        page.setRecords(list);

        return page;
    }

    @Override
    public int updateWzSpecialtySignRelation(WzSpecialtySignRelation relation) {
        return wzSpecialtySignRelationMapper.updateWzRelation(relation);
    }

    @Override
    public List<WzSpecialtySignRelation> selectDataAll() {
        return wzSpecialtySignRelationMapper.selectDataAll();
    }
}
